﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataTransferBetweenViews.Model
{
    public class Model : IModel
    {
        private string selectedPerson;
        private EventHandler favoriteBooksEvent;

        #region IModel Members

        /// <summary>
        /// Gets or sets the selected person.
        /// </summary>
        /// <value>The selected person.</value>
        string IModel.SelectedPerson
        {
            get
            {
                return this.selectedPerson;
            }

            set
            {
                this.selectedPerson = value;
                this.GetFavoriteBooks();
            }
        }

        /// <summary>
        /// Gets or sets the favorite books event.
        /// </summary>
        /// <value>The favorite books event.</value>
        EventHandler IModel.FavoriteBooksEvent
        {
            get
            {
                return this.favoriteBooksEvent;
            }
            set
            {
                this.favoriteBooksEvent = value;
            }
        }

        /// <summary>
        /// Gets the subscribed persons.
        /// </summary>
        /// <returns></returns>
        IList<string> IModel.GetSubscribedPersons()
        {
            IList<string> persons = new List<string>();
            // May be you can subscribed persons from persons database table.
            persons.Add("Ravi");
            persons.Add("Surya");

            return persons;
        }

        /// <summary>
        /// Gets the favorite books.
        /// </summary>
        private void GetFavoriteBooks()
        {
            IList<string> favoriteBooks = new List<string>();

            if ((this as IModel).SelectedPerson.Equals("Ravi", StringComparison.OrdinalIgnoreCase))
            {
                favoriteBooks.Add("Harry Potter");
                favoriteBooks.Add("Lord of the rings");
            }
            else if ((this as IModel).SelectedPerson.Equals("Surya", StringComparison.OrdinalIgnoreCase))
            {
                favoriteBooks.Add("*********");
                favoriteBooks.Add("---------");
                favoriteBooks.Add("&&&&&&&&&");
            }
            else
            {
            }

            // Ideally if you use MVVM pattern, there should not be any direct data transfer between views.
            // All data should be transfered using Model.
            (this as IModel).FavoriteBooksEvent.Invoke(favoriteBooks, null);
        }

        #endregion
    }
}
